package com.czxy.Servlet;

import cn.itcast.servlet.BaseServlet;
import com.czxy.Service.QianDaoService;
import com.czxy.domain.Qian;
import com.czxy.domain.User;
import com.czxy.utils.MyBatisUtils;
import net.sf.json.JSON;
import net.sf.json.JSONArray;
import net.sf.json.JSONString;

import javax.servlet.annotation.WebServlet;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author 嗨皮骚
 * @version v 1.0
 * @date 2019/5/9
 */
@WebServlet("/qiandao")
public class QianDaoServlet extends BaseServlet {
    QianDaoService service=new QianDaoService();
    public void selectDate() throws IOException {
        User loginUser = (User) getRequest().getSession().getAttribute("loginUser");
        String uid = loginUser.getUid();
        List<Qian> qians=service.findQianDaoByUid(uid);
        String s = JSONArray.fromObject(qians).toString();
        System.out.println(s);
        getResponse().getWriter().write(s);
    }
    public void addQianDao(){
        User loginUser = (User) getRequest().getSession().getAttribute("loginUser");
        String uid = loginUser.getUid();
        Date date = new Date();
        if (uid!=null){
            service.add(uid,date);
        }
    }
    public void findQianDaoByUidDate() throws ParseException, IOException {
        User loginUser = (User) getRequest().getSession().getAttribute("loginUser");
        String uid = loginUser.getUid();
        Date date = new Date();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        String format = simpleDateFormat.format(date);
        Date parse = simpleDateFormat.parse(format);
        int i=service.findQianDaoByUidDate(uid,parse);
        String i1=i+"";
        System.out.println(i1);
        getResponse().getWriter().write(i1);
    }
    public void userQianDaoNum() throws IOException {
        User loginUser = (User) getRequest().getSession().getAttribute("loginUser");
        int i=service.userQianDaoNum(loginUser);
        String i1=i+"";
        getResponse().getWriter().write(i1);
    }
    //更新
    public void upDatabaseDate(){
        service.upDatabaseDate();
    }
}
